<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-inFo
                :msg="dataId"
                :disabled="disableSubmit"
                v-decorator="['xingMing', validatorRules.xingMing]"
                :trigger-change="true"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['xingBie', validatorRules.xingBie]"
                :trigger-change="true"
                dictCode="gkak_xb"
                placeholder="请选择性别"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                :disabled="disableSubmit"
                placeholder="请选择日期"
                v-decorator="['riQi', validatorRules.riQi]"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="年龄" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number
                :disabled="disableSubmit"
                v-decorator="['nianLing', validatorRules.nianLing]"
                placeholder="请输入年龄"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['dianHua', validatorRules.dianHua]"
                placeholder="请输入电话"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['diZhi', validatorRules.diZhi]"
                placeholder="请输入地址"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="就诊医院" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['jiuZhenYiYuan', validatorRules.jiuZhenYiYuan]"
                placeholder="请输入就诊医院"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="就诊科室" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['jiuZhenKeShi', validatorRules.jiuZhenKeShi]"
                placeholder="请输入就诊科室"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="门诊病历号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['bingLiHao', validatorRules.bingLiHao]"
                placeholder="请输入门诊病历号"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="就诊医生" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['jiuZhenYiSheng', validatorRules.jiuZhenYiSheng]"
                placeholder="请输入就诊医生"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="配药" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['peiYao', validatorRules.peiYao]"
                placeholder="请输入配药"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number
                :disabled="disableSubmit"
                v-decorator="['jinE', validatorRules.jinE]"
                placeholder="请输入金额"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['fenLei', validatorRules.fenLei]"
                :trigger-change="true"
                dictCode="gkak_fl"
                placeholder="请选择分类"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="患病部位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['huanBingBuWei', validatorRules.huanBingBuWei]"
                :trigger-change="true"
                dictCode="gkak_hbbw"
                placeholder="请选择患病部位"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="检查内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['jianChaNeiRong', validatorRules.jianChaNeiRong]"
                placeholder="请输入检查内容"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="主诉内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['zhuSuNeiRong', validatorRules.zhuSuNeiRong]"
                placeholder="请输入主诉内容"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="既往史" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['jiWangShi', validatorRules.jiWangShi]"
                placeholder="请输入既往史"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="现病史" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['xianBingShi', validatorRules.xianBingShi]"
                placeholder="请输入现病史"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="检查结果" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['jianChaJieGou', validatorRules.jianChaJieGou]"
                placeholder="请输入检查结果"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="费别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="radio"
                v-decorator="['feiBie', validatorRules.feiBie]"
                :trigger-change="true"
                dictCode="gkak_fb"
                placeholder="请选择费别"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="诊断" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['zhenDuan', validatorRules.zhenDuan]"
                placeholder="请输入诊断"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="相关禁忌" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['xiangGuanJinJi', validatorRules.xiangGuanJinJi]"
                placeholder="请输入相关禁忌"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="12">
          <a-col :span="12">
            <a-form-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload
                :disabled="disableSubmit"
                v-decorator="['fuJian', validatorRules.fuJian]"
                :trigger-change="true"
              ></j-upload>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-form-item label="治疗处方" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-editor :disabled="disableSubmit" v-decorator="['zhiLiaoChuFang', { trigger: 'input' }]" />
          </a-form-item>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JDate from '@/components/jeecg/JDate'
import JUpload from '@/components/jeecg/JUpload'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import JEditor from '@/components/jeecg/JEditor'
import JSelectInFo from '@/components/jeecgbiz/JSelectInFo'

export default {
  name: 'GkakDaDianZiBingLiModal',
  components: {
    JDate,
    JUpload,
    JDictSelectTag,
    JSearchSelectTag,
    JEditor,
    JSelectInFo
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: '操作',
      dataId: {
        id: '',
        name: ''
      },
      width: 800,
      disableSubmit: false,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        xingMing: { rules: [] },
        xingBie: { rules: [] },
        riQi: { rules: [] },
        nianLing: { rules: [] },
        dianHua: { rules: [] },
        diZhi: { rules: [] },
        jiuZhenYiYuan: { rules: [{ required: true, message: '请输入就诊医院!' }] },
        jiuZhenKeShi: { rules: [{ required: true, message: '请输入就诊科室!' }] },
        bingLiHao: { rules: [] },
        jiuZhenYiSheng: { rules: [{ required: true, message: '请输入就诊医生!' }] },
        peiYao: { rules: [] },
        jinE: { rules: [] },
        fenLei: { rules: [{ required: true, message: '请输入分类!' }] },
        huanBingBuWei: { rules: [{ required: true, message: '请输入患病部位!' }] },
        jianChaNeiRong: { rules: [] },
        zhuSuNeiRong: { rules: [] },
        jiWangShi: { rules: [] },
        xianBingShi: { rules: [] },
        jianChaJieGou: { rules: [] },
        feiBie: { rules: [] },
        zhenDuan: { rules: [] },
        zhiLiaoChuFang: { rules: [] },
        xiangGuanJinJi: { rules: [] },
        fuJian: { rules: [] }
      },
      url: {
        add: '/gkakDaDianZiBingLi/add',
        edit: '/gkakDaDianZiBingLi/edit'
      }
    }
  },
  created() {
    this.$nextTick(() => {
      if (this.$route.query.jianKangXinXiId) {
        if (localStorage.getItem('jiangkangInfo')) {
          let row = JSON.parse(localStorage.getItem('jiangkangInfo'))
          console.log(row)
          this.form.setFieldsValue({
            jianKangXinXiId: row.id,
            xingMing: row.xingMing
          })
        }
      }
    })
  },
  methods: {
    inFoId(record) {
      
        this.dataId = {
          id: record.jianKangXinXiId,
          name: record.xingMing
        }
     
    },
    add() {
      this.edit({})
    },
    edit(record) {
     if (this.$route.query.jianKangXinXiId) {
        if (localStorage.getItem('jiangkangInfo')) {
          let row = JSON.parse(localStorage.getItem('jiangkangInfo'))
          console.log(row)
         this.dataId = {
          id: row.id,
          name:row.xingMing
        }
        }
      } else {
        this.inFoId(record)
      }

      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'xingMing',
            'xingBie',
            'riQi',
            'nianLing',
            'dianHua',
            'diZhi',
            'jiuZhenYiYuan',
            'jiuZhenKeShi',
            'bingLiHao',
            'jiuZhenYiSheng',
            'peiYao',
            'jinE',
            'fenLei',
            'huanBingBuWei',
            'jianChaNeiRong',
            'zhuSuNeiRong',
            'jiWangShi',
            'xianBingShi',
            'jianChaJieGou',
            'feiBie',
            'zhenDuan',
            'zhiLiaoChuFang',
            'xiangGuanJinJi',
            'fuJian'
          )
        )
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          if (this.$route.query.yongHuBiaoShi) {
            if (localStorage.getItem('jiangkangInfo')) {
              let row = JSON.parse(localStorage.getItem('jiangkangInfo'))
              console.log(row)
              values.yongHuBiaoShi = row.id
              values.xingMing = row.xingMing
            }
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'xingMing',
          'xingBie',
          'riQi',
          'nianLing',
          'dianHua',
          'diZhi',
          'jiuZhenYiYuan',
          'jiuZhenKeShi',
          'bingLiHao',
          'jiuZhenYiSheng',
          'peiYao',
          'jinE',
          'fenLei',
          'huanBingBuWei',
          'jianChaNeiRong',
          'zhuSuNeiRong',
          'jiWangShi',
          'xianBingShi',
          'jianChaJieGou',
          'feiBie',
          'zhenDuan',
          'zhiLiaoChuFang',
          'xiangGuanJinJi',
          'fuJian'
        )
      )
    }
  }
}
</script>
